home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / LIST.C < prev    next >
C/C++ Source or Header  |  1992-01-14  |  7KB  |  266 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/list.c,v 9.29 1992/01/15 02:33:17 jinx Exp $
  4.  
  5. Copyright (c) 1987-92 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* List creation and manipulation primitives. */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39.  
  40. DEFINE_PRIMITIVE ("PAIR?", Prim_pair, 1, 1, 0)
  41. {
  42.   fast SCHEME_OBJECT object;
  43.   PRIMITIVE_HEADER (1);
  44.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  45.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (PAIR_P (object)));
  46. }
  47.  
  48. SCHEME_OBJECT
  49. DEFUN (cons, (car, cdr),
  50.        SCHEME_OBJECT car
  51.        AND SCHEME_OBJECT cdr)
  52. {
  53.   Primitive_GC_If_Needed (2);
  54.   (*Free++) = car;
  55.   (*Free++) = cdr;
  56.   return (MAKE_POINTER_OBJECT (TC_LIST, (Free - 2)));
  57. }
  58.  
  59. DEFINE_PRIMITIVE ("CONS", Prim_cons, 2, 2, 0)
  60. {
  61.   PRIMITIVE_HEADER (2);
  62.   PRIMITIVE_RETURN (cons ((ARG_REF (1)), (ARG_REF (2))));
  63. }
  64.  
  65. DEFINE_PRIMITIVE ("CAR", Prim_car, 1, 1, 0)
  66. {
  67.   PRIMITIVE_HEADER (1);
  68.   CHECK_ARG (1, PAIR_P);
  69.   PRIMITIVE_RETURN (PAIR_CAR (ARG_REF (1)));
  70. }
  71.  
  72. DEFINE_PRIMITIVE ("CDR", Prim_cdr, 1, 1, 0)
  73. {
  74.   PRIMITIVE_HEADER (1);
  75.   CHECK_ARG (1, PAIR_P);
  76.   PRIMITIVE_RETURN (PAIR_CDR (ARG_REF (1)));
  77. }
  78.  
  79. DEFINE_PRIMITIVE ("SET-CAR!", Prim_set_car, 2, 2, 0)
  80. {
  81.   PRIMITIVE_HEADER (2);
  82.   CHECK_ARG (1, PAIR_P);
  83.   {
  84.     fast SCHEME_OBJECT pair = (ARG_REF (1));
  85.     fast SCHEME_OBJECT car = (ARG_REF (2));
  86.     SIDE_EFFECT_IMPURIFY (pair, car);
  87.     SET_PAIR_CAR (pair, car);
  88.   }
  89.   PRIMITIVE_RETURN (UNSPECIFIC);
  90. }
  91.  
  92. DEFINE_PRIMITIVE ("SET-CDR!", Prim_set_cdr, 2, 2, 0)
  93. {
  94.   PRIMITIVE_HEADER (2);
  95.   CHECK_ARG (1, PAIR_P);
  96.   {
  97.     fast SCHEME_OBJECT pair = (ARG_REF (1));
  98.     fast SCHEME_OBJECT cdr = (ARG_REF (2));
  99.     SIDE_EFFECT_IMPURIFY (pair, cdr);
  100.     SET_PAIR_CDR (pair, cdr);
  101.   }
  102.   PRIMITIVE_RETURN (UNSPECIFIC);
  103. }
  104.  
  105. /* (GENERAL-CAR-CDR LIST DIRECTIONS)
  106.    DIRECTIONS encodes a string of CAR and CDR operations to be
  107.    performed on LIST as follows:
  108.      1   = NOP    101 = CDAR
  109.      10  = CDR    110 = CADR
  110.      11  = CAR    111 = CAAR
  111.      100 = CDDR    ... */
  112.  
  113. DEFINE_PRIMITIVE ("GENERAL-CAR-CDR", Prim_general_car_cdr, 2, 2, 0)
  114. {
  115.   PRIMITIVE_HEADER (2);
  116.   {
  117.     fast SCHEME_OBJECT object = (ARG_REF (1));
  118.     fast long CAR_CDR_Pattern = (arg_nonnegative_integer (2));
  119.     while (CAR_CDR_Pattern > 1)
  120.       {
  121.     TOUCH_IN_PRIMITIVE (object, object);
  122.     if (! (PAIR_P (object)))
  123.       error_wrong_type_arg (1);
  124.     object =
  125.       (((CAR_CDR_Pattern & 1) == 0)
  126.        ? (PAIR_CDR (object))
  127.        : (PAIR_CAR (object)));
  128.     CAR_CDR_Pattern >>= 1;
  129.       }
  130.     PRIMITIVE_RETURN (object);
  131.   }
  132. }
  133.  
  134. DEFINE_PRIMITIVE ("LENGTH", Prim_length, 1, 1, 0)
  135. {
  136.   fast SCHEME_OBJECT list;
  137.   fast long i = 0;
  138.   PRIMITIVE_HEADER (1);
  139.  
  140.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), list);
  141.   while (PAIR_P (list))
  142.     {
  143.       i += 1;
  144.       TOUCH_IN_PRIMITIVE ((PAIR_CDR (list)), list);
  145.     }
  146.   if (list != EMPTY_LIST)
  147.     error_wrong_type_arg (1);
  148.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (i));
  149. }
  150.  
  151. DEFINE_PRIMITIVE ("MEMQ", Prim_memq, 2, 2, 0)
  152. {
  153.   fast SCHEME_OBJECT key;
  154.   fast SCHEME_OBJECT list;
  155.   fast SCHEME_OBJECT list_key;
  156.   PRIMITIVE_HEADER (2);
  157.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), key);
  158.   TOUCH_IN_PRIMITIVE ((ARG_REF (2)), list);
  159.   while (PAIR_P (list))
  160.     {
  161.       TOUCH_IN_PRIMITIVE ((PAIR_CAR (list)), list_key);
  162.       if (list_key == key)
  163.     PRIMITIVE_RETURN (list);
  164.       TOUCH_IN_PRIMITIVE ((PAIR_CDR (list)), list);
  165.     }
  166.   if (list != EMPTY_LIST)
  167.     error_wrong_type_arg (2);
  168.   PRIMITIVE_RETURN (SHARP_F);
  169. }
  170.  
  171. DEFINE_PRIMITIVE ("ASSQ", Prim_assq, 2, 2, 0)
  172. {
  173.   fast SCHEME_OBJECT key;
  174.   fast SCHEME_OBJECT alist;
  175.   fast SCHEME_OBJECT association;
  176.   fast SCHEME_OBJECT association_key;
  177.   PRIMITIVE_HEADER (2);
  178.  
  179.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), key);
  180.   TOUCH_IN_PRIMITIVE ((ARG_REF (2)), alist);
  181.   while (PAIR_P (alist))
  182.     {
  183.       TOUCH_IN_PRIMITIVE ((PAIR_CAR (alist)), association);
  184.       if (! (PAIR_P (association)))
  185.     error_wrong_type_arg (2);
  186.       TOUCH_IN_PRIMITIVE ((PAIR_CAR (association)), association_key);
  187.       if (association_key == key)
  188.     PRIMITIVE_RETURN (association);
  189.       TOUCH_IN_PRIMITIVE ((PAIR_CDR (alist)), alist);
  190.     }
  191.   if (alist != EMPTY_LIST)
  192.     error_wrong_type_arg (2);
  193.   PRIMITIVE_RETURN (SHARP_F);
  194. }
  195.  
  196. DEFINE_PRIMITIVE ("SYSTEM-PAIR?", Prim_sys_pair, 1, 1, 0)
  197. {
  198.   fast SCHEME_OBJECT object;
  199.   PRIMITIVE_HEADER (1);
  200.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  201.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (GC_PAIR_P (object)));
  202. }
  203.  
  204. SCHEME_OBJECT
  205. DEFUN (system_pair_cons, (type, car, cdr),
  206.        long type
  207.        AND SCHEME_OBJECT car
  208.        AND SCHEME_OBJECT cdr)
  209. {
  210.   Primitive_GC_If_Needed (2);
  211.   (*Free++) = car;
  212.   (*Free++) = cdr;
  213.   return (MAKE_POINTER_OBJECT (type, (Free - 2)));
  214. }
  215.  
  216. DEFINE_PRIMITIVE ("SYSTEM-PAIR-CONS", Prim_sys_pair_cons, 3, 3, 0)
  217. {
  218.   PRIMITIVE_HEADER (3);
  219.   {
  220.     long type = (arg_index_integer (1, (MAX_TYPE_CODE + 1)));
  221.     if ((GC_Type_Code (type)) != GC_Pair)
  222.       error_bad_range_arg (1);
  223.     PRIMITIVE_RETURN (system_pair_cons (type, (ARG_REF (2)), (ARG_REF (3))));
  224.   }
  225. }
  226.  
  227. DEFINE_PRIMITIVE ("SYSTEM-PAIR-CAR", Prim_sys_pair_car, 1, 1, 0)
  228. {
  229.   PRIMITIVE_HEADER (1);
  230.   CHECK_ARG (1, GC_PAIR_P);
  231.   PRIMITIVE_RETURN (PAIR_CAR (ARG_REF (1)));
  232. }
  233.  
  234. DEFINE_PRIMITIVE ("SYSTEM-PAIR-CDR", Prim_sys_pair_cdr, 1, 1, 0)
  235. {
  236.   PRIMITIVE_HEADER (1);
  237.   CHECK_ARG (1, GC_PAIR_P);
  238.   PRIMITIVE_RETURN (PAIR_CDR (ARG_REF (1)));
  239. }
  240.  
  241. DEFINE_PRIMITIVE ("SYSTEM-PAIR-SET-CAR!", Prim_sys_set_car, 2, 2, 0)
  242. {
  243.   PRIMITIVE_HEADER (2);
  244.   CHECK_ARG (1, GC_PAIR_P);
  245.   {
  246.     fast SCHEME_OBJECT pair = (ARG_REF (1));
  247.     fast SCHEME_OBJECT car = (ARG_REF (2));
  248.     SIDE_EFFECT_IMPURIFY (pair, car);
  249.     SET_PAIR_CAR (pair, car);
  250.   }
  251.   PRIMITIVE_RETURN (UNSPECIFIC);
  252. }
  253.  
  254. DEFINE_PRIMITIVE ("SYSTEM-PAIR-SET-CDR!", Prim_sys_set_cdr, 2, 2, 0)
  255. {
  256.   PRIMITIVE_HEADER (2);
  257.   CHECK_ARG (1, GC_PAIR_P);
  258.   {
  259.     fast SCHEME_OBJECT pair = (ARG_REF (1));
  260.     fast SCHEME_OBJECT cdr = (ARG_REF (2));
  261.     SIDE_EFFECT_IMPURIFY (pair, cdr);
  262.     SET_PAIR_CDR (pair, cdr);
  263.   }
  264.   PRIMITIVE_RETURN (UNSPECIFIC);
  265. }
  266.